import axios from 'axios';
import qs from 'qs';

const BASE_URL = 'https://sunflower.app.cloudendpoint.cn/';

const service = axios.create({
  baseURL: process.env.BASE_URL,
  timeout: 60000,
  headers: {},
});

service.interceptors.request.use(
  (config) => {
    return config;
  },
  (err) => {
    Promise.reject(err);
  }
);

service.interceptors.response.use(
  (data) => {
    return data;
  },
  (err) => {
    return Promise.reject(err.response);
  }
);

// 创建请求
export function createRequest(url, method, params) {
  return async function (rest) {
    const data = rest || {};
    const res = await service.request({
      url: BASE_URL + url,
      data,
      method,
      params,
      paramsSerializer: (params_1) => {
        return qs.stringify(params_1, { indices: false });
      },
    });
    if (res.data.code > 0) throw res;
    return res.data;
  };
}
